package minglue.util

import redis.clients.jedis.JedisCluster
import java.util.concurrent.ConcurrentLinkedDeque

object RedisCluster {
  private val connectionNum = 20 // 每次产生的连接数
  private val pool = new ConcurrentLinkedDeque[JedisCluster]() //连接池
  import java.util

   def hgetAll(key: String): util.Map[String, String] = {
    val jedisCluster = getJedisCluster
    val map =jedisCluster.hgetAll(key)
    closeJedisCluster(jedisCluster)
    map
  }

  def getJedisCluster(): JedisCluster = {
    AnyRef.synchronized({
      if (pool.isEmpty()) {
        for (i <- 1 to connectionNum) {
          val conn = RedisConnector.getConnection()
          pool.offer(conn) //添加一个元素 并返回true 如果队列已满则返回false
        }
      }
      pool.poll() //移除并返回队列头部元素 如果为空则阻塞

    })
  }

  def  closeJedisCluster(jedisCluster:JedisCluster): Unit = {
    pool.offer(jedisCluster)
  }
}
   